<?php

/**
 * This is the model class for table "usuario".
 *
 * The followings are the available columns in table 'usuario':
 * @property integer $id
 * @property string $login
 * @property string $password
 * @property integer $level
 * @property integer $status
 * @property string $nome
 * @property string $sobrenome
 * @property string $rg
 * @property string $foto
 * @property string $sexo
 * @property integer $profissao_id
 * @property string $documento
 * @property integer $tipoDocumentounico_id
 * @property integer $estadocivil_id
 * @property string $data_nascimento
 * @property integer $excluido
 * @property string $dataCadastro
 */
class Usuario extends CActiveRecord
{
	
	public $dia;
	public $mes;
	public $ano;
	public $imagem;
	public $alterarFoto = 0;
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuario';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('login, password, level, status, nome, sobrenome, sexo, profissao_id, documento, tipoDocumentounico_id, estadocivil_id, data_nascimento, dataCadastro', 'required'),
			array('level, status, profissao_id, tipoDocumentounico_id, estadocivil_id, excluido, alterarFoto, dia, mes, ano', 'numerical', 'integerOnly'=>true),
			array('foto', 'file', 'types' => 'jpg, jpeg, png', 'allowEmpty' => TRUE),
			array('login, nome', 'length', 'max'=>45),
			array('password, sobrenome', 'length', 'max'=>40),
			array('rg', 'length', 'max'=>15),
			array('foto', 'length', 'max'=>60),
			array('sexo', 'length', 'max'=>1),
			array('documento', 'length', 'max'=>20),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, login, password, level, status, nome, sobrenome, rg, foto, sexo, profissao_id, documento, tipoDocumentounico_id, estadocivil_id, data_nascimento, excluido, dataCadastro, alterarFoto, dia, mes, ano', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'login' => 'Login',
			'password' => 'Password',
			'level' => 'Level',
			'status' => 'Status',
			'nome' => 'Nome',
			'sobrenome' => 'Sobrenome',
			'rg' => 'Rg',
			'foto' => 'Foto',
			'sexo' => 'Sexo',
			'profissao_id' => 'Profissao',
			'documento' => 'Documento',
			'tipoDocumentounico_id' => 'Tipo Documentounico',
			'estadocivil_id' => 'Estadocivil',
			'data_nascimento' => 'Data Nascimento',
			'excluido' => 'Excluido',
			'dataCadastro' => 'Data Cadastro',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('login',$this->login,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('level',$this->level);
		$criteria->compare('status',$this->status);
		$criteria->compare('nome',$this->nome,true);
		$criteria->compare('sobrenome',$this->sobrenome,true);
		$criteria->compare('rg',$this->rg,true);
		$criteria->compare('foto',$this->foto,true);
		$criteria->compare('sexo',$this->sexo,true);
		$criteria->compare('profissao_id',$this->profissao_id);
		$criteria->compare('documento',$this->documento,true);
		$criteria->compare('tipoDocumentounico_id',$this->tipoDocumentounico_id);
		$criteria->compare('estadocivil_id',$this->estadocivil_id);
		$criteria->compare('data_nascimento',$this->data_nascimento,true);
		$criteria->compare('excluido',$this->excluido);
		$criteria->compare('dataCadastro',$this->dataCadastro,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Usuario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	
	public static function getNameLevel($id)
	{
		$level = Tipousuario::model()->findByPk($id);
		return $level->nome;
	}
	
	public static function getNameProfissao($id)
	{
		$profissao = Profissao::model()->findByPk($id);
		return $profissao->nome;
	}
	
	public static function getNameTipoDocumento($id)
	{
		$tipoDocumento = Tipodocumentounico::model()->findByPk($id);
		return $tipoDocumento->nome;
	}
	
	public static function getNameEstadoCivil($id)
	{
		$estadoCivil = Estadocivil::model()->findByPk($id);
		return $estadoCivil->nome;
	}
}
